/**
 * 
 */
package com.jeeplus.common.utils;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.FilterProvider;

/**
 * @author Administrator
 *
 */
public final class JsonTransformer {

	private final static ObjectMapper OBJ_MAPPER = new ObjectMapper();

	private JsonTransformer() {
	}

	public static <T> T json2Object(String value, Class<T> clazz) {

		if (StringUtils.isBlank(value)) {
			return null;
		}

		T t = null;
		try {
			t = OBJ_MAPPER.readValue(value, clazz);
		} catch (JsonParseException e) {

		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {

		}

		return t;
	}

	public static <T> String object2Json(T object) {

		if (object == null) {
			return null;
		}

		try {
			return OBJ_MAPPER.writeValueAsString(object);
		} catch (JsonParseException e) {

		} catch (JsonMappingException e) {

		} catch (IOException e) {

		}

		return null;
	}

	public static <T> String object2JsonByFilter(T object, FilterProvider filter) {

		if (object == null) {
			return null;
		}

		try {
			OBJ_MAPPER.setFilterProvider(filter);
			return OBJ_MAPPER.writeValueAsString(object);
		} catch (JsonParseException e) {

		} catch (JsonMappingException e) {

		} catch (IOException e) {

		}

		return null;
	}
}
